package cc.rengu.igas.channel.xmup.core.service.trans;


import cc.rengu.igas.channel.xmup.common.constant.XmupParamConstant;
import cc.rengu.igas.channel.xmup.common.constant.XmupTreeNodeConstant;
import cc.rengu.igas.channel.xmup.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.xmup.common.enums.XmupProdEnum;
import cc.rengu.igas.channel.xmup.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.xmup.core.service.base.PayService;
import cc.rengu.igas.channel.xmup.facade.request.RepayRequest;
import cc.rengu.igas.channel.xmup.facade.response.RepayResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 银联全渠道单笔代付服务 on 2019/10/8.
 */
public class SingleRepayService extends PayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        RepayRequest repayRequest = new RepayRequest();
        ConvertUtil.convertOutput(repayRequest);
        return repayRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        RepayResponse repayResponse = new RepayResponse();
        RepayRequest repayRequest = (RepayRequest) request;

        /*处理厦门银联请求报文*/
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(XmupTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);

        xmlTreeUtil.setXmlTreeStringValue(XmupTreeNodeConstant.application, "Pay.Req");
        xmlTreeUtil.setXmlTreeStringValue(XmupTreeNodeConstant.version, "1.1.0");

        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        if (98 == iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            repayResponse.setRespCode("04");
            repayResponse.setRespDesc("交易状态未明");
            chnlBizResponse.setResult(repayResponse);
            return chnlBizResponse;
        } else if (0 != iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(repayResponse);

        if (XmupParamConstant.SUCCESS_CODE.equals(repayResponse.getRespCode())) {
            repayResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            repayResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        }
        chnlBizResponse.setResult(repayResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        RepayResponse repayResponse = (RepayResponse) response.getResult();
        ConvertUtil.convertInput(repayResponse);
    }

}
